
# Copyright (c) 2014, Franklin Brauning V.  All rights reserved.
# 
#    El desarrollo inicial de esta utilidad fue producido
#    por Franklin Brauning <brauningbar@gmail.com>
# 
#    Si consideras que este codigo puede serte util, eres libre
#    de usarlo a conveniencia, pero no olvides mantener la licencia
#    y dar credito al autor.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#   * Redistributions of source code must retain the above copyright notice, this
#     list of conditions and the following disclaimer.
# 
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
# 
#   * Neither the name of the author nor the names of its contributors may
#     be used to endorse or promote products derived from this software without
#     specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from django.db import models
from .base import *
from .strings import *


def show_deleted(func):
	''' composable __str__decorator to show a toggle-on-deleted
	on deletable models
	'''
	def w(self):
		s,ats = func(self)
		if self.deleted:
			ats.append("eliminado")
		return s,ats
	return w


class deletable(abstractmodel):
	''' enrich a model to keep track of deletes instead of
	performing the actual delete
	'''
	deleted = models.BooleanField(verbose_name=strdeleted, default=False,
	  editable=False)
	class Meta:
		abstract = True

	def delete(self, *a, **kw):
		''' delete this deletable object.
		Does nothing if already deleted
		'''
		if self.deleted:
			self.trace("deletable.delete:not deleting already deleted model")
		else:
			self.trace("deletable.delete")
			self.deleted = True
			self.save()

	def undelete(self):
		''' mark current object as not-deleted and then .save

		Does nothing if model is not currently deleted'''
		if self.deleted:
			self.trace("deletable.undelete")
			self.deleted = False
			self.save()
		else:
			self.trace("deletable.undelete:not resurrecting already alive model")

	def rawdelete(self, *a, **kw):
		''' delete this deletable object using stock .save
		Does nothing if already deleted
		'''
		if self.deleted:
			self.trace("deletable.rawdelete:not deleting already deleted model")
		else:
			self.trace("deletable.rawdelete")
			self.deleted = True
			self.rawsave()

	def rawundelete(self):
		''' same as .undelete but the model is saved with the stock
		.save method
		'''
		if self.deleted:
			self.trace("deletable.rawundelete")
			self.deleted = False
			self.rawsave()
		else:
			self.trace("deletable.rawundelete:not resurrecting already alive model")

	def begin_span_deletable(self):
		c = "_deleted" if self.deleted else "_active"
		t = ("title='%s'" % self.title()) if hasattr(self, 'title') else ''
		return "<span id='item-%d' data-item-id=%d class='item %s %s' %s>" % \
		(self.id, self.id, c, self._meta.model_name, t)


